package org.dtrd.modules.flup.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.dtrd.base.BaseController;
import org.dtrd.common.api.vo.Result;
import org.dtrd.modules.base.service.BaseCommonService;
import org.dtrd.modules.flup.entity.bean.FlupAttentionDetail;
import org.dtrd.modules.flup.service.IDtrdEntRdFlupAttentionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import static org.dtrd.common.constant.CommonConstant.*;
import static org.dtrd.config.system.SystemState.ResponseState.STATE_OPERATION_FAILURE;
import static org.dtrd.config.system.SystemState.ResponseState.STATE_PARAMS_MISSING;

/**
 * <p>
 * 复诊注意事项 前端控制器
 * </p>
 * @author QR
 * @since 2021-12-29
 */
@Api(tags = "复诊注意事项")
@RestController
@RequestMapping("/flup/attention/")
public class FlupAttentionController extends BaseController {

    @Autowired
    private IDtrdEntRdFlupAttentionService flupAttentionService;
    @Autowired
    private BaseCommonService baseCommonService;

    @ApiOperation(value = "查询最新的复诊注意事项", response = FlupAttentionDetail.class)
    @GetMapping("get")
    public Result<?> getFlupAttention(){
        FlupAttentionDetail info = flupAttentionService.getFlupAttentionLatest();
        String logContent = "查询最新的复诊注意事项";
        baseCommonService.addLog(logContent, OPERATE_TYPE_QUERY);
        return Result.OK(info);
    }

    @ApiOperation("新增或修改复诊注意事项")
    @PostMapping("edit")
    public Result<?> saveOrUpdateFlupAttention(@RequestBody FlupAttentionDetail dto){
        boolean result = flupAttentionService.saveOrUpdateFlupAttention(dto);
        String logContent = String.format("新增或修改复诊注意事项, 修改内容: %s", dto.getAttentionContent());
        baseCommonService.addLog(logContent, OPERATE_TYPE_SAVE_OR_UPDATE);
        return result ? Result.OK() : error(STATE_OPERATION_FAILURE);
    }

    @ApiOperation("删除复诊注意事项")
    @DeleteMapping("remove")
    public Result<?> removeFlupAttention(@RequestParam Integer id){
        if(isIntParamInvalid(id)) {
            return error(STATE_PARAMS_MISSING);
        }
        boolean result = flupAttentionService.removeFlupAttention(id);
        String logContent = String.format("删除复诊注意事项, 删除id: %d", id);
        baseCommonService.addLog(logContent, OPERATE_TYPE_DELETE);
        return result ? Result.OK() : error(STATE_OPERATION_FAILURE);
    }
}
